package cn.edu.lingnan.translation.controller;


import cn.edu.lingnan.translation.entity.Admin;
import cn.edu.lingnan.translation.entity.User;
import cn.edu.lingnan.translation.entity.vo.AdminLoginVo;
import cn.edu.lingnan.translation.entity.vo.AdminQueryVo;
import cn.edu.lingnan.translation.service.AdminService;
import cn.edu.lingnan.translation.utils.MD5;
import cn.edu.lingnan.translation.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Bao
 * @since 2020-10-10
 */
@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;

    /**
     * @Description 管理员登录
     * @Param [adminVo]
     * @Return cn.edu.lingnan.translation.utils.R
     * @Author BAO
     * @Date 2020/10/11 12:55
     */
    @ApiOperation(value = "管理员登录")
    @PostMapping("login")
    public R login(@RequestBody AdminLoginVo adminVo) {
        String id = adminService.login(adminVo);
        return R.ok().data("id", id);
    }

    /**
     * @Description 获取管理员分页列表
     * @Param [page, limit, adminQueryVo]
     * @Return cn.edu.lingnan.translation.utils.R
     * @Author BAO
     * @Date 2020/10/14 9:12
     */
    @ApiOperation(value = "获取管理员分页列表")
    @PostMapping("/{page}/{limit}")
    public R getAdminPage(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,
            @ApiParam(name = "adminQueryVo", value = "查询对象", required = false)
            @RequestBody AdminQueryVo adminQueryVo) {
        // 分页
        Page<Admin> adminPage = new Page<>(page, limit);
        // 条件查询
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(adminQueryVo.getUsername())) {
            wrapper.like("username", adminQueryVo.getUsername());
        }
        if(!StringUtils.isEmpty(adminQueryVo.getMobile())) {
            wrapper.like("mobile", adminQueryVo.getMobile());
        }
        if(!StringUtils.isEmpty(adminQueryVo.getNickname())) {
            wrapper.like("nickname", adminQueryVo.getNickname());
        }
        IPage<Admin> iPage = adminService.page(adminPage, wrapper);
        return R.ok().data("admins", iPage.getRecords()).data("total", iPage.getTotal());
    }

    /**
     * @Description 通过id获取信息
     * @Param [id]
     * @Return cn.edu.lingnan.translation.utils.R
     * @Author BAO
     * @Date 2020/10/11 16:35
     */
    @ApiOperation(value = "通过id查找管理员信息")
    @GetMapping("/{id}")
    public R getAdminById(@PathVariable String id) {
        Admin admin = adminService.getById(id);
        return R.ok().data("admin", admin);
    }

    /**
     * @Description 管理员更新
     * @Param [admin]
     * @Return cn.edu.lingnan.translation.utils.R
     * @Author BAO
     * @Date 2020/10/14 9:07
     */
    @ApiOperation(value = "管理员更新")
    @PostMapping("/save")
    public R saveAdminById(@RequestBody Admin admin) {
        admin.setPassword(MD5.encrypt(admin.getPassword()));
        adminService.updateById(admin);
        return R.ok();
    }

    /**
     * @Description 管理员删除
     * @Param [id]
     * @Return cn.edu.lingnan.translation.utils.R
     * @Author BAO
     * @Date 2020/10/14 15:06
     */
    @ApiOperation(value = "管理员删除")
    @DeleteMapping("/{id}")
    public R saveAdminById(@PathVariable String id) {
        adminService.removeById(id);
        return R.ok();
    }

}

